﻿using FyAutoStartService.Models;
using FySystem.Secure;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace FyAutoStartService
{
    internal class Authentication
    {
        /// <summary>
        /// 用户认证
        /// </summary>
        /// <param name="user"></param>
        /// <returns></returns>
        /// <exception cref="NotImplementedException"></exception>
        internal static bool Auth(UserInfo? user)
        {
            bool res = false;
            try
            {
                if (user == null)
                    return res;

                if (string.IsNullOrWhiteSpace(Global.Username) || string.IsNullOrWhiteSpace(Global.Passwd))
                {
                    //如果配置文件中的账号和密码都是空的，则不管客户端传入的用户名和密码是什么，都认为通过
                    res = true;
                }
                else
                {
                    if (Global.Username == user.Username)
                    {
                        if (!string.IsNullOrWhiteSpace(user.Password))
                        {
                            if (user.Password.ToUpper() == MD5.MD5Encrypt(Global.Passwd).ToUpper())
                                res = true;
                        }
                    }
                }
            }
            catch (Exception)
            {
                throw;
            }
            return res;
        }
    }
}
